package com.hans.scala.elem

abstract class Element {
  val height = contents.length
  val width =
    if (height == 0) 0 else contents(0).length

  def contents: Array[String]

  def above(that: Element): Element =
    new ArrayElement(this.contents ++ that.contents)

  def beside(that: Element): Element =
    new ArrayElement(
      for (
      /**
       * zip解释
       * Array(1, 2, 3) zip Array("a", "b")
       * Array((1, "a"), (2, "b"))
       */
        (line1, line2) <- this.contents zip that.contents
      ) yield line1 + line2
    )

  override def toString = contents mkString "\n"
}

object Element {
  def main(args: Array[String]) {
    val space = elem(" ")
    val corner = elem("+")

    val ae = elem("hello")
    val e1 = elem(Array("hello", "world"))
    val e2 = ae
    val e3 = elem('x', 2, 3)
    val e4 = e2.above(e1)
    println("end")
  }

  def elem(contents: Array[String]): Element =
    new ArrayElement(contents)

  def elem(chr: Char, width: Int, height: Int): Element =
    new UniformElement(chr, width, height)

  def elem(line: String): Element =
    new LineElement(line)
}
